package com.sg.hms.controller;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sg.hms.Model.Getpath;
import com.sg.hms.Model.User;
import com.sg.hms.Service.RoomService;
import com.sg.hms.ServiceImpl.RoomServiceImpl;
/***
 * 添加房间物品
 * @author 吴磊
 *
 */
public class AddRoomController extends HttpServlet{
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		req.setCharacterEncoding("UTF-8");
		int rId=Integer.parseInt(req.getParameter("rId"));
		int eId=Integer.parseInt(req.getParameter("eId"));
		int count=Integer.parseInt(req.getParameter("count"));
		String state=req.getParameter("state");
		RoomService room=new RoomServiceImpl();
		String string="房间编号："+rId+"物品编号"+eId+"物品数量："+count+"物品状态："+state;
		User user=(User) req.getSession().getAttribute("currentuser");
		int uid=user.getuId();
		String fpath=Getpath.getpath1();
		StringBuffer stringBuffer=new StringBuffer(fpath+"/Add");
		stringBuffer.append(String.valueOf(uid));
		Date date=new Date();
		SimpleDateFormat sim=new SimpleDateFormat("yyyyMMddHHmmss");
		String now=sim.format(date);
		stringBuffer.append(now);
		stringBuffer.append(".txt");
		String path=stringBuffer.toString();
		File file=new File(path);
		file.createNewFile();
		FileWriter writer=new FileWriter(file, true);
		writer.write(string);
		writer.flush();
		writer.close();
		int tem= room.addRoom(rId, eId, count, state);
		Writer writer2=resp.getWriter();
		writer2.write(String.valueOf(tem));
		writer2.flush();
		writer2.close();
	}

}
